/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
/*
 * ScheduleReportSource.java
 *
 * Created on 07 April 2006, 06:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package blueprint4j.report.db;

import blueprint4j.db.*;
import blueprint4j.utils.*;

import java.sql.SQLException;

/**
 *
 * @author Jasper van der Bijl
 * The reports for a scheduled report to be sent out
 */
public class ScheduleReportSource extends Entity {
    
    static final String TABLE_NAME = "d_sch_rpt_source";
    
    public TableName table_name = new TableName(TABLE_NAME,this);
    
    public FieldGroup group_rpt_source = new FieldGroup("Report",this);
    
    public FieldAutoId id = new FieldAutoId("id", 0, "The Record Identifier", this);
    public FieldStringSelect report = new FieldStringSelect("report",Field.FIELD_NOT_NULL,"Report",200,ScheduleReport.REPORT_NAME,ScheduleReport.REPORT_SOURCE,this);
    public FieldString file_name = new FieldString("file_name",Field.FIELD_NAME | Field.FIELD_NOT_NULL,"Email filename",50,this);
    public FieldStringSelect export_type = new FieldStringSelect("export_type",0,"Report Type",20,new String[]{"PDF","XLS","CSV","HTML"},this);
    
    public FieldGroupDisable group_disbale = new FieldGroupDisable(this);
    
    public FieldForeignKey schedule_report_id = new FieldForeignKey("schedule_report_id",0,"Schedule Report",26,ScheduleReport.class,this);

    /** Creates a new instance of ScheduleReportSource */
    public ScheduleReportSource() {
        super();
    }
    
    public ScheduleReportSource(DBConnection dbcon) throws SQLException {
        super(dbcon);
    }
    
    public Entity getNewInstance() {
        return new ScheduleReportSource();
    }
    
    public void save()
    throws BindException {
        if (shouldSave()) {
            if (file_name.get() == null || file_name.get().length() == 0) {
                file_name.set("report");
            }
            if ((export_type.get() == null || export_type.get().equals("PDF")) && !file_name.get().endsWith(".pdf")) {
                file_name.set(file_name.get() + ".pdf");
            } else if ((export_type.get() != null && export_type.get().equals("HTML")) && !file_name.get().endsWith(".html")) {
                file_name.set(file_name.get() + ".html");
            } else if ((export_type.get() != null && export_type.get().equals("CSV")) && !file_name.get().endsWith(".csv")) {
                file_name.set(file_name.get() + ".csv");
            } else if ((export_type.get() != null && export_type.get().equals("XLS")) && !file_name.get().endsWith(".xls")) {
                file_name.set(file_name.get() + ".xls");
            }
            super.save();
        }
    }    
}
